package com.airtight.programs;

import android.content.Context;
import android.opengl.GLES20;

import static android.opengl.GLES20.GL_TEXTURE0;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.glActiveTexture;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform1i;
import static android.opengl.GLES20.glUniformMatrix4fv;

public class TextureShaderProgram extends ShaderProgram{
    private final int uMatrixLocation;
    private final int uTextureUnitLocation;
    private final int uOpacity;
    // Attribute locations
    private final int aPositionLocation;
    private final int aTextureLocation;

    public TextureShaderProgram(Context context, int textureShader, int fragmentShader) {
        //creates shader program with simple vertex and fragment shader
        super(context, textureShader, fragmentShader);
        // Retrieve uniform locations for the shader program.
        uMatrixLocation         =  glGetUniformLocation(program, "u_Matrix");
        uTextureUnitLocation    =  glGetUniformLocation(program, "u_TextureUnit");
        uOpacity                =  glGetUniformLocation(program, "u_Opacity");
        // Retrieve attribute locations for the shader program.
        aPositionLocation       = glGetAttribLocation(program, "a_Position");
        aTextureLocation        = glGetAttribLocation(program, "a_TextureCoordinates");
    }
    /**sets the matrix uniform to passed in matrix*/
    public void setTexture(float[] matrix, int textureId) {
        // Pass the matrix into the shader program.
        glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
        // Set the active texture unit to texture unit 0.
        glActiveTexture(GL_TEXTURE0);
        // Bind the texture to this unit.
        glBindTexture(GL_TEXTURE_2D, textureId);
        // Tell the texture uniform sampler to use this texture in the shader by
        // telling it to read from texture unit 0.
        glUniform1i(uTextureUnitLocation, 0);
    }

    public void setOpacity(float transparency) {
        GLES20.glUniform1f(uOpacity, transparency);
    }

    public int getPosition() {
        return aPositionLocation;
    }
    public int getTextureLocation() {
        return aTextureLocation;
    }
}
